﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace Microsoft.Practices.ObjectBuilder2
{
	public class NamedTypeBuildKey
	{
		// Fields
		private readonly string name;
		private readonly Type type;

		// Methods
		public NamedTypeBuildKey(Type type)
			: this(type, null)
		{
		}

		public NamedTypeBuildKey(Type type, string name)
		{
			this.type = type;
			this.name = !string.IsNullOrEmpty(name) ? name : null;
		}

		public override bool Equals(object obj)
		{
			NamedTypeBuildKey key = obj as NamedTypeBuildKey;
			if (key == null)
			{
				return false;
			}
			return (this == key);
		}

		public override int GetHashCode()
		{
			int num = (this.type == null) ? 0 : this.type.GetHashCode();
			int num2 = (this.name == null) ? 0 : this.name.GetHashCode();
			return ((num + 0x25) ^ (num2 + 0x11));
		}

		public static NamedTypeBuildKey Make<T>()
		{
			return new NamedTypeBuildKey(typeof(T));
		}

		public static NamedTypeBuildKey Make<T>(string name)
		{
			return new NamedTypeBuildKey(typeof(T), name);
		}

		public static bool operator ==(NamedTypeBuildKey left, NamedTypeBuildKey right)
		{
			bool flag = object.ReferenceEquals(left, null);
			bool flag2 = object.ReferenceEquals(right, null);
			if (flag && flag2)
			{
				return true;
			}
			if (flag || flag2)
			{
				return false;
			}
			return ((left.type == right.type) && (string.Compare(left.name, right.name, StringComparison.Ordinal) == 0));
		}

		public static bool operator !=(NamedTypeBuildKey left, NamedTypeBuildKey right)
		{
			return !(left == right);
		}

		public override string ToString()
		{
			object[] args = new object[] { this.type, this.name ?? "null" };
			return string.Format(CultureInfo.InvariantCulture, "Build Key[{0}, {1}]", args);
		}

		// Properties
		public string Name
		{
			get
			{
				return this.name;
			}
		}

		[SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Justification = "This is the type part of the key.")]
		public Type Type
		{
			get
			{
				return this.type;
			}
		}
	}


}
